package org.meproj.vanet.console;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.math.BigDecimal;

import org.client.nosql.redis.RedisUtil;
import org.meproj.communication.bean.SignalType;
import org.meproj.communication.bean.VanetSignal;
import org.pocs.common.dto.Signal;


public class FileReader {

	private String fileLocation;
	private static String TIME = "Time";
	private static int TIME_START_LOC = 16;
	
	private static String SOURCE = "Source";

	private static String SEND_HELLO_PACKET = "Broadcasting a hello message";
	private static String RECEIVE_HELLO_PACKET = "Received a hello packet";

	

	private static String HELLO_PACKET = "hello packet";


	private static int START_LOC = 0;
	private static int END_LOC = 0;
	
	
	public FileReader(String fileLocation) {
		// TODO Auto-generated constructor stub
		this.fileLocation = fileLocation;
	}
	
	public void readFileLine() throws IOException {
	
		RandomAccessFile accessFile = new RandomAccessFile(fileLocation,"rw");
		String currLine = "";
		VanetSignal signal = null;
		long count = 0;
		RedisUtil redisUtil = new RedisUtil();
		
		while ((currLine = accessFile.readLine()) != null ) {
			//System.out.println(currLine);
			
			try {
				if(currLine.contains(TIME)) {
					
						END_LOC = currLine.indexOf("sec");
						String time = currLine.substring(TIME_START_LOC, END_LOC).trim();
						signal = new VanetSignal();
						signal.setTimeStamp(new BigDecimal(time));
					
				} else if(currLine.contains(SOURCE)) {

					START_LOC = currLine.indexOf("Office Network.") + "Office Network.".length();
					END_LOC = currLine.indexOf(".", START_LOC);

					signal.setSourceNode(currLine.substring(START_LOC, END_LOC));
					
				} else if (currLine.contains(SEND_HELLO_PACKET)) {
					
					signal.setToNode(Signal.toNodeNULL);
					signal.setFromNode(signal.getSourceNode());
					
					signal.setSignalType(SignalType.SEND);
					
					redisUtil.add(signal.getTimeStamp().toString(), signal);
					System.out.println(signal);
					count++;
					
				} else if (currLine.contains(RECEIVE_HELLO_PACKET)) {
					START_LOC = currLine.indexOf("Office Network.") + "Office Network.".length();
					END_LOC = currLine.indexOf(")");
					
					signal.setFromNode(currLine.substring(START_LOC, END_LOC));
					signal.setToNode(signal.getSourceNode());
					
					signal.setSignalType(SignalType.RECEIVE);
					
					
					redisUtil.add(signal.getTimeStamp().toString(), signal);
					System.out.println(signal);
					count++;
//				}  

//					START_LOC = currLine.indexOf("Wireless Subnet_0.");
//					END_LOC = currLine.indexOf(".arp");
//
//					signal.setFromNode(currLine.substring(START_LOC, END_LOC));
					
				} else if (currLine.contains(HELLO_PACKET)) {
					START_LOC = currLine.indexOf("Wireless Subnet_0.");
					END_LOC = currLine.indexOf(")");
					
					signal.setToNode(currLine.substring(START_LOC, END_LOC));
					
					redisUtil.add(signal.getTimeStamp().toString(), signal.toString());
					count++;
				}

			} catch (Exception e) {
				//System.out.println("Current Line: " + currLine);
				//System.out.println("Start Location: " + START_LOC + "; End Location: " + END_LOC);
				//e.printStackTrace();
				signal = new VanetSignal();
			}
			
		}
		
		accessFile.close();
		System.out.println("Rows added to Redis: " + count);
	}
	
	public static void main(String[] args) {

		FileReader fileReader = new FileReader("/home/vms/codeRepository/Opnet/ConsoleReaderForVanet/src/project12_1_2014");

	//	FileReader fileReader = new FileReader("C:\\RiteshRootFolder\\console1");

		try {
			fileReader.readFileLine();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}
